#include "factory.h"

int factory_init(Factory_t *pf,int thread_num,int capacity)
{
    que_init(&pf->que,capacity);
    pf->start_flag=0;
    int ret=pthread_cond_init(&pf->cond,NULL);
    THREAD_ERR_CHECK(ret,"pthread_cond_init")
    pf->pthid=(pthread_t *)calloc(thread_num,sizeof(pthread_t));
    return 0;
}
int factory_start(Factory_t * pf,int thread_num)
{
    if(!pf->start_flag)
    {
        int i;
        for(i=0;i<thread_num;i++)
        {
            pthread_create(pf->pthid+i,NULL,thread_handle,pf);
        }
        pf->start_flag=1;
    }
    return 0;
}
void cleanup(void *p)
{
pthread_mutex_unlock((pthread_mutex_t*)p);
}
void *thread_handle(void* p)
{
    Factory_t* pf=(Factory_t*)p;
    pQue_t pq=&pf->que;
    pNode_t pcur=NULL;
    while (1)
    {
        pthread_mutex_lock(&pq->mutex);
        pthread_cleanup_push(cleanup,&pq->mutex)
        if(0==pq->que_size)
        {
            pthread_cond_wait(&pf->cond,&pq->mutex);

        }
        que_get(pq,&pcur);
        pthread_cleanup_pop(1);
        if(pcur)
        {
            tran_file(pcur->new_fd);
            free(pcur);
            pcur=NULL;
        }


    }
    
}